package com.hui.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hui.config.FindFile;
import com.hui.entity.Book;
import com.hui.entity.News;
import com.hui.mapper.NewsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.File;
import java.util.List;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hui.service
 * @Author: HUI
 * @CreateTime: 2021-12-08 11:36
 * @Description:
 */

@Service
@Slf4j
public class NewsService {
    @Autowired
    NewsMapper newsMapper;


    //分页模糊查询
    public Page<News> queryBook(Integer pageNum,
                                Integer pageSize,
                                String searchStr) {

        Page<News> newsPage = new Page<>(pageNum, pageSize);


        //lambda 模糊查询：user 表中名字相似于 search 的

        LambdaQueryWrapper<News> wrapper = Wrappers.<News>lambdaQuery();
        if (!searchStr.isBlank()) {
            wrapper.like(News::getTitle, searchStr);
        }


        return newsMapper.selectPage(newsPage, wrapper);


    }

    //新增
    public int saveUser(News news) {
        LambdaQueryWrapper<News> wrapper = Wrappers.<News>lambdaQuery().eq(News::getTitle, news.getTitle());
        News news1 = newsMapper.selectOne(wrapper);

        return news1 == null ? newsMapper.insert(news) : -1;
      /*  if (books!=null){
            return -1;
        }else {
            return newsMapper.insert(book);
        }*/

    }


    //修改
    public int updateBook(@RequestBody News news) {
        return newsMapper.updateById(news);
    }

    //删除下架
    public int deleteBook(Integer id) {
//        News news = newsMapper.selectById(id);
        return newsMapper.deleteById(id);

    }


    //查询自己发布的所有新闻
    public List<News> QueryPushNews(String author){
        LambdaQueryWrapper<News> wrapper = Wrappers.<News>lambdaQuery().eq(News::getAuthor, author);
        return newsMapper.selectList(wrapper);
    }
}
